package com.example.docking.service.impl;

import com.example.docking.dao.jindie.GenericDataItem;
import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.request.InvPickRequest;
import com.example.docking.dao.jindie.response.*;
import com.example.docking.service.InvPickDataService;
import com.example.docking.util.Constants;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @MethodName
 * @Parameters 查询生产领料的
 * @Author zhanghuiping
 * @Date 2025 - 05 - 06 06:17:43
 */


public class InvPickDataServiceImpl extends DataServiceImpl implements InvPickDataService {
    private static final Logger logger = LoggerFactory.getLogger(EmpServiceImpl.class);

    @Override
    public List<InvPickData> selectList() {
        Gson gson = new Gson();
        // 设置分页参数
        int page = 1; // 初始页码
        int pageSize = 10; // 每页记录数
        HashMap<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        map.put("page_size", String.valueOf(pageSize));
        List<InvPickData> allData = null;
        ApiResult DataResult = null;
        DataServiceImpl Service = new DataServiceImpl();
        // 调用分页查询方法获取职员数据
        try {
            DataResult = Service.SelectDataList(Constants.INV_PICK, map, "");

            Type DataType = new TypeToken<ResponseData<GenericDataItem<InvPickData>>>() {
            }.getType();
            ResponseData<GenericDataItem<InvPickData>> responseData = gson.fromJson(DataResult.getBody(), DataType);
            logger.info("查询返回code:" + responseData.getErrcode());
            logger.info("查询返回description:" + responseData.getDescription());
            GenericDataItem<InvPickData> genericDataItem = responseData.getData();
            // 解析分页信息
            String count = genericDataItem.getCount();
            int countNum = Integer.valueOf(count);
            int Pagesum = pageSize;
            List<InvPickData> Data = genericDataItem.getRows();

            allData = new ArrayList<>(Data);
            // 如果不是最后一页，继续获取下一页数据
            while (Pagesum < countNum) {
                page++;
                map.put("page", String.valueOf(page));
                DataResult = Service.SelectDataList(Constants.INV_PICK, map, "");
                responseData = gson.fromJson(DataResult.getBody(), DataType);
                genericDataItem = responseData.getData();
                Data = genericDataItem.getRows();
                allData.addAll(Data);
                Pagesum = (pageSize * page);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return allData;
    }

    @Override
    public ResponseData insertInvPickData(InvPickRequest invPickData) {
        Gson gson = new Gson();
        // 设置分页参数
        int page = 1; // 初始页码
        int pageSize = 10; // 每页记录数
        HashMap<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        map.put("page_size", String.valueOf(pageSize));
        ApiResult DataResult = null;
        DataServiceImpl Service = new DataServiceImpl();
        // 调用分页查询方法获取职员数据
        try {
            DataResult = Service.InsertDataOne(Constants.INV_PICK, map, invPickData);

            Type DataType = new TypeToken<ResponseData<GenericDataItem<InvPickData>>>() {
            }.getType();
            ResponseData<GenericDataItem<InvPickData>> responseData = gson.fromJson(DataResult.getBody(), DataType);
            logger.info("查询返回code:" + responseData.getErrcode());
            logger.info("查询返回description:" + responseData.getDescription());
            GenericDataItem<InvPickData> genericDataItem = responseData.getData();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public static InvPickDetailData selectById(String id) {
        InvPickDetailData invPickDetailData;
        Gson gson = new Gson();
        logger.info("根据编号获取采购订单");
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        DataServiceImpl Service = new DataServiceImpl();
        ApiResult DataResult = null;
        try {
            DataResult = Service.SelectDataList(Constants.INV_PICK_DETAIL, map, "");
            Type SaleDataType = new TypeToken<ResponseData<InvPickDetailData>>() {
            }.getType();
            ResponseData<InvPickDetailData> responseSaleData = gson.fromJson(DataResult.getBody(), SaleDataType);
            invPickDetailData = responseSaleData.getData();
            logger.info("code" + responseSaleData.getErrcode());
            logger.info("描述" + responseSaleData.getDescription());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return invPickDetailData;
    }

    public static void main(String[] args) {
        InvPickDataServiceImpl service = new InvPickDataServiceImpl();
        List<InvPickData> data = service.selectList();
        System.out.println(data);
        for (InvPickData invPickData : data) {
            System.out.println(selectById(invPickData.getId()));
        }
//        InvPickRequest invPickData = new InvPickRequest();
//        invPickData.setBill_date("2025-05-07");
//        List<MaterialEntity> materialEntityList = new ArrayList<>();
//        MaterialEntity materialEntity = new MaterialEntity();
//        materialEntity.setMaterial_id("2199706160989249536");
//        materialEntity.setQty(8);
//        materialEntity.setStock_id("2179251571911575552");
//        materialEntity.setUnit_id("2198047493676101632");
//        materialEntityList.add(materialEntity);
//        invPickData.setMaterial_entity(materialEntityList);
//        service.insertInvPickData(invPickData);

    }
}
